package com.xmkj.bearbiz.member;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.common.BearBean.ChuZhiBean;
import com.common.BearBean.JiFenListBean;
import com.common.BearBean.OrderListInfo;
import com.common.BearMethods.GetChuZhirListMethods;
import com.common.BearMethods.GetJiFenListMethods;
import com.common.BearMethods.GetOrderListMethods;
import com.common.adapter.helper.IRecyclerViewHelper;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.utils.SizeUtils;
import com.common.widget.DividerDecoration.MarginDecoration;
import com.common.widget.recyclerview.refresh.recycleview.XRecyclerView;
import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.member.adaper.ChuZhiAdapter;
import com.xmkj.bearbiz.member.adaper.JiFenAdapter;
import com.xmkj.bearbiz.member.adaper.UserOrderAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChuJiOrderListActivity extends BaseMvpActivity {
    public static final String TYPE = "type";
    public static final String VIPCARDID = "VIPCARDID";
    public static final String DEPTCODE = "DEPTCODE";
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerview;
    private int type;
    List<OrderListInfo> bean = new ArrayList<>();
    List<ChuZhiBean> chuzhibean = new ArrayList<>();
    List<JiFenListBean> jifenBean = new ArrayList<>();
    private UserOrderAdapter adapter;
    private String vipcardid;
    private String deptcode;
    private ChuZhiAdapter chuZhiAdapter;
    private JiFenAdapter jifenAdapter;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ji_chu_order;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        vipcardid = getIntent().getStringExtra(VIPCARDID);
        deptcode = getIntent().getStringExtra(DEPTCODE);

        if(type==1){ // 订单
            tv1.setText("日期时间");
            tv2.setText("订单编号");
            tv3.setText("件数");
            tv4.setText("金额");
            tv5.setText("赠送积分");
            tv6.setText("抵扣积分");
            gotoHttpRep();
            setRecyclerView();
        }else if(type==2){ // 储值
            tv6.setVisibility(View.GONE);
            tv1.setText("日期");
            tv2.setText("初始额");
            tv3.setText("储值变化");
            tv4.setText("备注");
            tv5.setText("余额");
            gotoChuZhiHttpRep();
            setChuZhiRecyclerView();
        }else if(type==3){
            tv6.setVisibility(View.GONE);
            tv1.setText("日期时间");
            tv2.setText("积分初始额");
            tv3.setText("积分变化");
            tv4.setText("备注");
            tv5.setText("积分余额");
            gotoJifenHttpRep();
            setJifenRecyclerView();
        }

    }

    private void setJifenRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        jifenAdapter = new JiFenAdapter(context, jifenBean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(jifenAdapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoJifenHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoJifenHttpRep();
            }
        });
    }

    private void gotoJifenHttpRep() {
        CommonSubscriber<ArrayList<JiFenListBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<JiFenListBean> beans = (ArrayList<JiFenListBean>) o;

                List<JiFenListBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    jifenAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    jifenBean = beanList;
                    jifenAdapter.addAll(jifenBean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                jifenAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                jifenAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetJiFenListMethods.getInstance().getScoreList(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), mPageIndex,vipcardid,deptcode);
        rxManager.add(subscriber);
    }

    private void setChuZhiRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        chuZhiAdapter = new ChuZhiAdapter(context, chuzhibean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(chuZhiAdapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoChuZhiHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoChuZhiHttpRep();
            }
        });
    }

    private void gotoChuZhiHttpRep() {
        CommonSubscriber<ArrayList<ChuZhiBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<ChuZhiBean> beans = (ArrayList<ChuZhiBean>) o;

                List<ChuZhiBean> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    chuZhiAdapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    chuzhibean = beanList;
                    chuZhiAdapter.addAll(chuzhibean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                chuZhiAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                chuZhiAdapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetChuZhirListMethods.getInstance().getSSMoney(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), mPageIndex,vipcardid,deptcode);
        rxManager.add(subscriber);
    }

    private void setRecyclerView() {
        IRecyclerViewHelper.init().setRecycleLineLayout(context, LinearLayoutManager.VERTICAL, recyclerview);
        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setHasFixedSize(true);
        recyclerview.setRefreshing(false);
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.HORIZONTAL, (int) SizeUtils.dp2px(context, 1)));
//        recyclerview.addItemDecoration(new MarginDecoration(LinearLayoutManager.VERTICAL, (int) SizeUtils.dp2px(context, 1)));
        recyclerview.setLoadMoreEnabled(true);
        adapter = new UserOrderAdapter(context, bean);
//        adapter.setOnItemClickSeclect(this);
        recyclerview.setAdapter(adapter);
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mPageIndex = 1;
                mIsRefreshOrLoadMore = 0;
                gotoHttpRep();
            }

            @Override
            public void onLoadMore() {
                if (mIsHasNoData) {
                    recyclerview.loadMoreComplete();
                    recyclerview.setNoMore(true);
                    return;
                }
                mPageIndex++;
                mIsRefreshOrLoadMore = 1;
                gotoHttpRep();
            }
        });
    }

    private void    gotoHttpRep() {
        CommonSubscriber<ArrayList<OrderListInfo>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                ArrayList<OrderListInfo> beans = (ArrayList<OrderListInfo>) o;

                List<OrderListInfo> beanList = beans;
                recyclerview.loadMoreComplete();
                // 下拉刷新
                if (mIsRefreshOrLoadMore == 0) {
                    recyclerview.refreshComplete();
                    adapter.clear();
                }

                if (EmptyUtils.isNotEmpty(beanList)) {
                    bean = beanList;
                    adapter.addAll(bean);
                } else if (mIsRefreshOrLoadMore == 0) {
//                    statusEmpty();
                    return;
                }

                if (EmptyUtils.isEmpty(beanList)) {
                    recyclerview.setNoMore(true);
                } else {
                    mIsHasNoData = beanList.size() < mPageSize;
                    recyclerview.setNoMore(mIsHasNoData);
                }
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                adapter.notifyDataSetChanged();
                recyclerview.refreshComplete();
                recyclerview.loadMoreComplete();
                if (mIsRefreshOrLoadMore == 0) {
                    dismissProgressDialog();
                }
            }
        });


        GetOrderListMethods.getInstance().getOrderList(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), mPageIndex,vipcardid,deptcode

        );
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        type = getIntent().getIntExtra(TYPE, Integer.MAX_VALUE);
        switch (type) {
            case 1:
                setNavigationBack("用户订单");
                break;
            case 2:
                setNavigationBack("储值详情");
                break;
            case 3:
                setNavigationBack("积分详情");
                break;
        }
    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
